<?php
class Driver extends MY_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('Mdriver');
    }
    
    public function index() {
        //Pre-load View
        $this->_data['load_page'] = 'driver/index_view';        
        if ($this->input->post('yt0')) {
            $driver = $this->input->post('Driver');
            //Form validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('Driver[name]', 'Name', 'required|callback_check_driver_exist');
            $this->form_validation->set_rules('Driver[email]', 'Email', 'required|valid_email');
            if ($this->form_validation->run()) {                
                //Check and Upload File
                $config['upload_path'] = './uploads/drivers/';
        		$config['allowed_types'] = 'gif|jpg|png|svg';
        		$config['max_size']	= '10240';      
        		$this->load->library('upload', $config);
        
        		if ( ! $this->upload->do_upload('fUpload'))
        		{
        			$this->_data['data']['error'] = $this->upload->display_errors();
        		}
        		else
        		{  		           
        			$upload_data = $this->upload->data();
                    $driver['avatar'] = $upload_data['file_name'];     
                    $this->Mdriver->insertDriver($driver);
                    $this->session->set_flashdata('flash_mess', 'Driver has been created successfully.'); 
                    redirect(base_url('driver/manage'));
        		}
            } else {
                $this->_data['data']['error'] = validation_errors('<li>', '</li>');
            }
        }
        //Load View
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function update($id) {
        //Pre-load View
        $this->_data['load_page'] = 'driver/update_view';
        $this->_data['data'] = $this->Mdriver->getDriverById($id);
        $this->_data['data_id'] = $id;
        //Process        
        if ($this->input->post('yt0')) {
            $driver = $this->input->post('Driver');
            //Form validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('Driver[name]', 'Name', 'required');
            if ($this->form_validation->run()) {                
                //Check and Upload File
                if ($this->input->post('oldImg') != $_FILES['fUpload']['name']) {
                    echo "<pre>";print_r($_FILES['fUpload']);echo "</pre>";
                    $config['upload_path'] = './uploads/drivers/';
            		$config['allowed_types'] = 'gif|jpg|png|svg';
            		$config['max_size']	= '10240';      
            		$this->load->library('upload', $config);
                    //Uploading
            		if ( ! $this->upload->do_upload('fUpload')) {
            		  $this->_data['data']['error'] = $this->upload->display_errors();
            		} else {
            		  $upload_data = $this->upload->data();
            		  $driver['avatar'] = $upload_data['file_name'];  
            		}
                } 		           
                if (empty($this->_data['data']['error'])) {
                    $this->Mdriver->updateDriver($driver, $id);
                    $this->session->set_flashdata('flash_mess', 'Driver has been updated successfully.'); 
                    redirect(base_url('driver/manage'));  
                }            
        		
            } else {
                $this->_data['data']['error'] = validation_errors('<li>', '</li>');
            }
        }
        //Load View        
        $this->load->view($this->_data['template'], $this->_data);       
    }
    
    public function display($page="0") {
        $this->_data['load_page'] = 'driver/display_view';
        //Pagination   
        $config['base_url'] = base_url('driver/display/');
        $config['total_rows'] = $this->Mdriver->countAll();
        $config['per_page'] = 10; 
        $this->load->library('pagination', $config);
        $this->_data['pagination_links'] = $this->pagination->create_links();
        //Load View
        $this->_data['data']['driver'] = $this->Mdriver->listAllDrivers($config['per_page'], $page, 'id desc');
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function manage($page="0") {
        //Pre-load View
        $this->_data['load_page'] = 'driver/manage_view'; 
        //Pagination   
        $config['base_url'] = base_url('driver/manage/');
        $config['total_rows'] = $this->Mdriver->countAll();
        $config['per_page'] = 10; 
        $this->load->library('pagination', $config);
        $this->_data['pagination_links'] = $this->pagination->create_links();
        //Load View
        //echo '<pre>';
        //print_r($driver);echo'</pre>';exit();
        $this->_data['current_page'] = $page;
        $this->_data['data']['driver'] = $this->Mdriver->listAllDrivers($config['per_page'], $page, 'id desc');  
        $this->_data['flash_mess'] = $this->session->flashdata('flash_mess');  
        $this->load->view($this->_data['template'], $this->_data);
    }

    
    public function delete($id) {
        $this->Mdriver->deleteDriver($id);
        redirect(base_url('driver/manage'));
    }
    
    public function check_name_exist() {
        $input = $this->input->post("Driver");
        if ($this->Mdriver->getDriverByName($input['name']) != 0) {
            $this->form_validation->set_message('check_name_exist', 'Name has been created in system. Please choose another');
            return false;
        } else {
            return true;
        }
    }
}